package com.unohacha.qqj.fzdr.personal.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;
import com.qpg.superhttp.SuperHttp;
import com.qpg.superhttp.callback.SimpleCallBack;
import com.qpg.widget.common.ToastUtil;
import com.unohacha.qqj.fzdr.R;
import com.unohacha.qqj.fzdr.bean.BaseBean;
import com.unohacha.qqj.fzdr.common.bean.ReleaseManagementBean;
import com.unohacha.qqj.fzdr.ui.activity.PubSettlementActivity;
import com.unohacha.qqj.fzdr.ui.activity.supply.EquipmentSupplyActivity;
import com.unohacha.qqj.fzdr.util.Prefer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ReleaseAdapter extends BaseQuickAdapter<ReleaseManagementBean, BaseViewHolder> {

    private String type="";

    public ReleaseAdapter(int layoutResId, @Nullable List<ReleaseManagementBean> data) {
        super(layoutResId, data);

    }

    public void setLabel(String type){
        this.type=type;
    }
    @Override
    protected void convert(final BaseViewHolder helper, final ReleaseManagementBean item) {
        //total-总条数;
        // current_page-当前页;
        // last_page-总页数;
        // id-记录id;
        // supply_cate_id-分类id;
        // title-内容;
        // list_pic-图片;
        // common_nums-评价数;
        // hits-查看量;
        // msg-是否到期提示语;
        // collection_count-收藏量;
        // create_time-发布时间;
        // status-0-下架；1-上架
        helper.setText(R.id.item_release_title, item.getTitle())
                .setText(R.id.item_release_time, item.getCreate_time())
                .setText(R.id.item_release_tv_collect_number, item.getCollection_count())
                .setText(R.id.item_release_tv_comment_number, item.getCommon_nums());
        if (item.getMsg().equals("已失效")) {
            helper.setTextColor(R.id.item_release_tv_remaining_time, mContext.getResources().getColor(R.color.text_color_red));
            helper.setText(R.id.item_release_tv_remaining_time, item.getMsg());
            helper.setGone(R.id.item_release_btn_release_undercarriage, false);
            helper.setGone(R.id.item_release_ll_release_invalid, true);
        } else {
            helper.setTextColor(R.id.item_release_tv_remaining_time, mContext.getResources().getColor(R.color.green1ccf70));
            helper.setText(R.id.item_release_tv_remaining_time, "剩余" + item.getMsg());
            helper.setGone(R.id.item_release_btn_release_undercarriage, true);
            helper.setGone(R.id.item_release_ll_release_invalid, false);
        }

        //九宫格展示
        ArrayList<ImageInfo> imageInfo = new ArrayList<>();
        List<String> imageDetails = item.getList_pic();
        if (imageDetails != null) {
            for (String imageDetail : imageDetails) {
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(imageDetail);
                info.setBigImageUrl(imageDetail);
                imageInfo.add(info);
            }
        }
        NineGridView nineGridView = helper.getView(R.id.item_release_rv_picture);

        nineGridView.setAdapter(new NineGridViewClickAdapter(mContext, imageInfo));

        helper.addOnClickListener(R.id.item_release_btn_release_undercarriage);
        helper.getView(R.id.item_release_btn_release_undercarriage).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sid=item.getSupply_cate_id();
                String id=item.getId();
                dismountInfo(helper,sid,id);
            //    Log.d(TAG, "onClick: " + "信息下架");
            }
        });
        helper.getView(R.id.item_release_btn_release_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             //   Log.d(TAG, "onClick: " + "删除信息");
                String sid=item.getSupply_cate_id();
                String id=item.getId();
                deleteInfo(helper,sid,id);

            }
        });
        helper.getView(R.id.item_release_btn_release_again).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //   Log.d(TAG, "onClick: " + "消息重发");
                rePub(item);
            }
        });
    }

    private void deleteInfo(final BaseViewHolder helper, String sid, String id){
        SuperHttp.post("api/mycenter/delSupplyInformation")
                .addParam("token", Prefer.getInstance().getToken())
                .addParam("supply_cate_id",sid)
                .addParam("id",id)
                .request(new SimpleCallBack<BaseBean<Object>>() {
                    @Override
                    public void onSuccess(BaseBean<Object> data) {
                        ToastUtil.showCustomToast(data.getMessage());
                        if(data.getCode()==1){
                            getData().remove(helper.getLayoutPosition());
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }
    private void dismountInfo(final BaseViewHolder helper, String sid, String id){
        SuperHttp.post("api/mycenter/soldOutSupplyInformation")
                .addParam("token", Prefer.getInstance().getToken())
                .addParam("supply_cate_id",sid)
                .addParam("id",id)
                .request(new SimpleCallBack<BaseBean<Object>>() {
                    @Override
                    public void onSuccess(BaseBean<Object> data) {
                        ToastUtil.showCustomToast(data.getMessage());
                        if(data.getCode()==1){
                            getData().remove(helper.getLayoutPosition());
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {

                    }
                });
    }

    private void rePub(ReleaseManagementBean item){
        HashMap<String,String> map = new HashMap<>();
        if(type.contains("面料")){
            map.put("token",Prefer.getInstance().getToken());
            map.put("title",item.getTitle());
            map.put("supply_cate_id",item.getSupply_cate_id());
            map.put("typeid","1");

            map.put("price","");
            map.put("price_company","");
            map.put("nums",item.getCommon_nums());
            map.put("nums_company","");
            map.put("breadth","");
            map.put("breadth_company","");
            map.put("technology","");
            map.put("breadth_company","");
            map.put("content","");
            map.put("matching","");
            map.put("gram_weight","");
            map.put("gram_weight_company","");
            map.put("component","");
            map.put("fabric_model","");
            map.put("id",item.getId());
            map.put("list_pic", "");
        }else if(type.contains("织造")){
            map.put("token", Prefer.getInstance().getToken());
            map.put("title", item.getTitle());
            map.put("supply_cate_id", item.getSupply_cate_id());
            map.put("typeid", "1");
            map.put("nums", "");
            map.put("nums_company", "");
            map.put("models", "");
            map.put("models_company", "");
            map.put("breadth", "");
            map.put("breadth_company", "");
            map.put("pointer_loom", "");
            map.put("flower_nums", "");
            map.put("multi_beam", "");
            map.put("content", "");
            map.put("matching","");
            map.put("spec", "");
            map.put("id",item.getId());
            map.put("list_pic", "");
        }else if(type.contains("原料")){
            map.put("token",Prefer.getInstance().getToken());
            map.put("title",item.getTitle());
            map.put("supply_cate_id",item.getSupply_cate_id());
            map.put("typeid","1");
            map.put("spec","");
            map.put("spec_company","");
            map.put("nums","");
            map.put("nums_company","");
            map.put("price","");
            map.put("price_company","");
            map.put("content","");
            map.put("matching","");
            map.put("id",item.getId());
            map.put("list_pic", "");
        }else if(type.contains("招聘")){
            map.put("token",Prefer.getInstance().getToken());
            map.put("title",item.getTitle());
            map.put("supply_cate_id",item.getSupply_cate_id());
            map.put("typeid","1");
            map.put("nums","");
            map.put("description","");
            map.put("salary","");
            map.put("salary_company","");
            map.put("education","");
            map.put("experience","");
            map.put("working_area","");
            map.put("content","");
            map.put("town_id","");
            map.put("matching","");
            map.put("company_basic_information","");
            map.put("id",item.getId());
            map.put("list_pic", "");
        }else if(type.contains("求职")){
            map.put("token", Prefer.getInstance().getToken());
            map.put("title",item.getTitle());
            map.put("supply_cate_id",item.getSupply_cate_id());
            map.put("typeid","1");
            map.put("realname","");
            map.put("sex","");
            map.put("age","");
            map.put("salary","");
            map.put("salary_company","");
            map.put("education","");
            map.put("experience","");
            map.put("expected_region","");
            map.put("content","");
            map.put("town_id","");
            map.put("matching","");
            map.put("the_advantage_of_me","");
            map.put("id",item.getId());
        }else if(type.contains("租赁")){

        }else if(type.contains("纺织设备")){

        }else if(type.contains("设备维修")){
            map.put("token",Prefer.getInstance().getToken());
            map.put("title",item.getTitle());
            map.put("supply_cate_id",item.getSupply_cate_id());
            map.put("typeid","1");
            map.put("price","");
            map.put("price_company","");
            map.put("nums","");
            map.put("nums_company","");
            map.put("trademark","");
            map.put("bs_type","");
            map.put("model_number","");
            map.put("factory_time","");
            map.put("producing_area","");
            map.put("content","");
            map.put("matching","");
            map.put("fault","");
        }else if(type.contains("库存")){

        }else if(type.contains("辅料")){

        }else if(type.contains("后道处理")){

        }else if(type.contains("其他")){

        }
        Intent intent=new Intent(mContext, PubSettlementActivity.class);
        intent.putExtra("params",map);
        mContext.startActivity(intent);
    }
}
